<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/24
 * Time: 下午3:57
 */

namespace app\admin\controller;

use app\common\model\Base;
use think\Db;
use think\Request;

/**
 *
 * 微信管理
 */
class Wx extends Basecontroller
{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user');

    }

    /**
     * 用户列表
     */
    public function user_list()
    {
        //搜索
        $request = Request::instance();
       // $currentDate_s_e = getthemonth(date("Y-m-d"));
        $pageQuery = [];
        $option['is_deleted'] = ['<', 3];
        //$option['doctor_id'] = $this->doctor->id;

        //默认年月日
       // $pageQuery['query']['start'] = $currentDate_s_e[0];

       // $pageQuery['query']['end'] = $currentDate_s_e[1];

        if ($request->isGet()) {
            $data = $request->param();

            if (isset($data['name']) && trim($data['name'])) {
                $option['name|id'] = array('like', '%' . trim($data['name']) . '%');
                $pageQuery['query']['name'] = trim($data['name']);

            }
            if (isset($data['start']) && $data['start'] !== '') {


                $last_date =  date('Y-m-d',strtotime($data['start']));
                $now_date =  date('Y-m-d',time());
                $sencond_data = $data['end'] ? $data['end'] : $now_date;
                $end =  date('Y-m-d',strtotime($sencond_data.'+1 day'));

                $option['create_time'] = array('between', ["{$last_date}", "{$end}"]);
                $pageQuery['query']['start'] = trim($data['start']);
                $pageQuery['query']['end'] = trim($data['end']);

            }
        }
      //  print_r($option);
        $this->assign('search', isset($pageQuery['query']) ? $pageQuery['query'] : '');
        $list = DB::name('user')
            ->where($option)
            ->order('create_time desc')
            ->paginate(false, false, $pageQuery)->each(function ($item, $key) {
                $optionnew['userid'] = $item['id'];
                $optionnew['is_deleted'] = 1;
                $item['record_count'] = $this->getall_Userfamilycasecount($optionnew);
                $item['family_count'] = $this->getUserfamilycount($optionnew);
                return $item;
            });

       //print_r(DB::name('user')->getLastSql());
        $this->assign('list', $list);
        $this->assign('url', $this->request->action());
        $this->assign('breadcrumb', '用户列表');
        return $this->fetch();

    }

    /**
     *编辑用户
     */
    public function edit_user()
    {
        $request = Request::instance();

        if ($request->isGet()) {
            $data_id = $request->param('uid');
            $user_info = $this->getUser_info(array('id' => $data_id));
            $this->assign('user_infos', $user_info);
        }
        $this->assign('breadcrumb', '编辑用户');
        if ($this->request->isPost()) {
            $form_data =$this->request->param();
            if(isset($form_data['password']) && $form_data['password']){
                $form_data['password']  = md5($form_data['password'] );
            }

            $form_data['id'] =$request->param('uid');
            $old_data = Base::getOneOldDataByTables($form_data['id'],model('user')->base_arr);
            Base::operaTable( model('user'), $this->userId, 'edit', $form_data, '', $old_data, model('user')->attach_arr, 'id', 1);
            $this->success('编辑成功',url('Wx/user_list'));

        }
        $this->assign('cancel_url','Wx/user_list');
        $this->assign('breadcrumb', '编辑用户');
        return $this->fetch();


    }

    /**
     * 删除用户
     */
    public function del_user()
    {

        if ($this->request->isPost()) {
            $form_data =$this->request->param();


            if(isset($form_data['is_deleted'])){
                Db::execute("update user set is_deleted=?  where id=?", [$form_data['is_deleted'], $form_data['id']]);
            }else{
                Db::execute("update user set is_deleted=3  where id=?", [ $form_data['id']]);
            }


            $old_data = Base::getOneOldDataByTables($form_data['id'],model('user')->base_arr);

            Base::operaTable( model('user'), $this->userId, 'del', $form_data, '', $old_data, model('user')->attach_arr, 'id', 1);
            $this->success('操作成功',url('Wx/user_list'));

        }

    }

    /**
     * 用户家庭成员列表
     */
    public function user_family_list()
    {
        $request = Request::instance();

        if ($request->isGet()) {
            $data = $request->param();

            $where['is_deleted'] = ['<',3] ;
            $where['userid'] =  $data['uid'] ;

           // $where = ['is_deleted' => self::DELETE_NO, 'userid' => $data['uid']];
            $user_info = $this->getUser_info(['id' => $data['uid']]);



            //默认年月日
            $pageQuery['query']['id'] = $data['uid'];

            $list = model('userfamily')
                ->where($where)
                ->order('create_time desc')
                ->paginate(false, false, $pageQuery)->each(function ($item, $key) {
                    $userfamily_info = $this->getPatientID_Bydocnum($item['document_num']);
                    $option['patient_id'] = $userfamily_info['id'];
                    $option['is_deleted'] = 1;
                    $item['patient_id'] = $userfamily_info['id'];
                    $item['record_count'] = $this->getUserfamilycase_count($option);
                    return $item;
                });


            $this->assign('userinfo', $user_info);
            $this->assign('list', $list);

        }
//        var_dump($list);
        $this->assign('url', $this->request->action());
        $this->assign('breadcrumb', '家庭成员列表');
        return $this->fetch();

    }

    /**
     * 文章说明
     */
    public function article()
    {
        $article_yhzn = $this->getArticle(['type' => 1, 'is_deleted' => self::DELETE_NO]);
        if ($article_yhzn) {
            $url1 = url('Wx/edit_article', array('type' => 1));
        } else {
            $url1 = url('Wx/add_article', array('type' => 1));

        }
        $this->assign('url1', $url1);
        $article_gywm = $this->getArticle(['type' => 2, 'is_deleted' => self::DELETE_NO]);
        if ($article_gywm) {
            $url2 = url('Wx/edit_article', array('type' => 2));
        } else {
            $url2 = url('Wx/add_article', array('type' => 2));

        }
        $this->assign('url2', $url2);
        $article_zn = $this->getArticle(['type' => 3, 'is_deleted' => self::DELETE_NO]);
        if ($article_zn) {
            $url3 = url('Wx/edit_article', array('type' => 3));
        } else {
            $url3 = url('Wx/add_article', array('type' => 3));

        }
        $this->assign('url3', $url3);
        $this->assign('breadcrumb', '文章说明');
        return $this->fetch();

    }

    /**
     * 添加文章
     */
    public function add_article()
    {
        if ($this->request->isAjax()) {
            $form_data = $this->request->param();
            $result = Base::operaTable(model('article'), $this->userId, 'add', $form_data, '', [], model('article')->attach_arr, 'id', 1);
            $this->success('添加成功', url('article'));
        }
        $this->assign('cancel_url', 'wx/article');
        $this->assign('breadcrumb', '添加文章');
        return $this->fetch();

    }

    /**
     * 添加文章
     */
    public function edit_article()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param('type');
            $article = $this->getArticle(['type' => $data, 'is_deleted' => self::DELETE_NO]);
            $this->assign('article', $article);
        }

        if ($this->request->isPost()) {
            $form_data = $this->request->param();
            $old_data = Base::getOneOldDataByTables($form_data['id'], model('article')->base_arr);
            Base::operaTable(model('article'), $this->userId, 'edit', $form_data, '', $old_data, model('article')->attach_arr, 'id', 1);
            $this->success('操作成功', url('article'));
        }

        $this->assign('cancel_url', 'wx/article');
        $this->assign('breadcrumb', '编辑文章');
        return $this->fetch('add_article');

    }

    /**
     * 反馈列表
     */
    public function notes()
    {
        //搜索
        $request = Request::instance();
        $pageQuery = [];

        //  print_r($option);
        $list = model('feedback')
            ->field('feedback.*,user.name ,user.phone')
            ->join('user', 'user.id=feedback.userid')
            ->order('feedback.is_read asc,feedback.create_time desc')
            ->paginate(false, false, $pageQuery);
        $this->assign('list', $list);
        $this->assign('url', $this->request->action());
        $this->assign('breadcrumb', '反馈列表');
        return $this->fetch();

    }

    /**
     * 反馈详情
     */
    public function notes_detail()
    {
        if ($this->request->isGet()) {
            $fid = $this->request->param('fid');
            $feedback = $this->getFeedback($fid);

            $this->assign('feedback', $feedback);
        }

        $this->assign('breadcrumb', '反馈详情');
        return $this->fetch();

    }


    public function send_msg()
    {   $controllerName = strtolower(CONTROLLER_NAME);
        $actionName = strtolower(ACTION_NAME);
        if ($this->request->isPost()) {
            $userids=$this->request->param('selectuser');
            if($userids==''){
                $this->error('发送短信对象不能为空');
//                return ['flag'=>'error','msg'=>'发送短信对象不能为空','url'=>''];
            }else{
                $user_id_arr = explode(',',$userids);
//                var_dump($user_id_arr);
            }
//            var_dump($userids);
//            die();
        }
            //验证是否要发送短信+短信之后的回复情况
        $this->checkSendMsg($controllerName,$actionName,$user_id_arr,$this->isSendMsg);


    }
}